package com.zyw.service.personnel.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zyw.entity.Clothing;
import com.zyw.entity.PersonnelStaff;
import com.zyw.from.personnelFrom;
import com.zyw.mapper.personnel.PersonnelStaffMapper;
import com.zyw.service.personnel.PersonnelStaffService;
import com.zyw.util.UnitConversionUtil;
import com.zyw.vo.LayUITableVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class PersonnelStaffServiceImpl implements PersonnelStaffService {

    @Resource
    public PersonnelStaffMapper personnelStaffMapper;


    @Override
    public LayUITableVO<List<PersonnelStaff>> findPage(Map<String, Object> map) {

        PageHelper.startPage((int) map.get("page"),(int) map.get("limit"));

        List<PersonnelStaff> list = personnelStaffMapper.findPage(map);
        for (PersonnelStaff personnelStaff : list) {
            convert(personnelStaff);
        }


        PageInfo<PersonnelStaff> pageInfo = new PageInfo<>(list);

        LayUITableVO<List<PersonnelStaff>> vo = new LayUITableVO<>();
        vo.setCount(pageInfo.getTotal());
        vo.setData(pageInfo.getList());
        return vo;
    }

    @Override
    public PersonnelStaff selectById(String id) {
        return personnelStaffMapper.selectByPrimaryKey(id);
    }

    @Override
    @Transactional
    public void update(PersonnelStaff personnelStaff) {
        personnelStaffMapper.updateByPrimaryKeySelective(personnelStaff);
    }

    @Override
    @Transactional
    public void save(PersonnelStaff personnelStaff) {
        personnelStaffMapper.insertSelective(personnelStaff);
    }

    @Override
    @Transactional
    public void delelteById(String id) {
        personnelStaffMapper.deleteByPrimaryKey(id);
    }

    @Override
    public List<PersonnelStaff> findAllIdAndName() {
        return personnelStaffMapper.findAllIdAndName();
    }


    public void convert(PersonnelStaff personnelStaff){
//        personnelStaff.setBirthDate(UnitConversionUtil.conversionDate(personnelStaff.getBirthDate()));
//        personnelStaff.setEntryDate(UnitConversionUtil.conversionDate(personnelStaff.getEntryDate()));

        if (personnelStaff.getStatus() == 0){
            personnelStaff.setStatusString("已离职");
        }else if (personnelStaff.getStatus() == 1){
            personnelStaff.setStatusString("在职");
        }else if (personnelStaff.getStatus() == 2){
            personnelStaff.setStatusString("请假中");
        }

    }
}
